<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class UpOld
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 报单区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->upPrice();
            $this->upVip();
            $this->upChain();
        }

    }

    public function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        if ($amount >= $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();

                // 取出待返
                $this->saveCommissionAwait($this->user->id);
            }
        }

        if ($amount >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }
    }

    // 这个是升V1
    public function upVip()
    {
        $r = Chain::getReferrer($this->user->id, true);
        if ($r && $r->level >= UserLevel::$keys['vip1']) {
            if ($r->vip < 1) {
                $has_vip1 = (int) User::find()
                    ->where(['referrer_id' => $r->id])
                    ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
                    ->count();
                if ($has_vip1 >= 2) {
                    $r->vip = 1;
                    $r->save();
                }
            }
            if ($r->vip < 2) {
                $has_vip1 = (int) User::find()
                    ->where(['referrer_id' => $r->id])
                    ->andWhere(['>=', 'level', UserLevel::$keys['vip2']])
                    ->count();
                if ($has_vip1 >= 2) {
                    $r->vip = 2;
                    $r->save();
                }
            }
        }
    }

    public function upChain()
    {
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3']
        ], true);

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip2']) {
                continue;
            }

            $this->_upChain($user);
        }
    }

    private function _upChain(User $user)
    {
        if ($user->star < 6) {
            $this->_exe($user, 5, $this->set->up_6);
        }
        if ($user->star < 5) {
            $this->_exe($user, 4, $this->set->up_5);
        }
        if ($user->star < 4) {
            $this->_exe($user, 3, $this->set->up_4);
        }
        if ($user->star < 3) {
            $this->_exe($user, 2, $this->set->up_3);
        }
        if ($user->star < 2) {
            $this->_exe($user, 1, $this->set->up_2);
        }
        if ($user->star < 1) {
            // $need_count = 2;
            $need_count = $this->set->up_1;
            $count = User::find()
                ->where(['level' => [
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3']
                ]])
                ->andWhere(['referrer_id' => $user->id])
                ->count();
            if ($count >= $need_count) {
                $user->star = 1;
                $user->save();
            }
        }

    }

    private function _exe(User $user, $star, $need_count)
    {
        // 后台配置了
        // $need_count = 2;

        $count = User::find()
            ->where(['level' => [
                UserLevel::$keys['vip2'],
                UserLevel::$keys['vip3']
            ]])
            ->andWhere(['referrer_id' => $user->id])
            ->andWhere(['>=', 'star', $star])
            ->count();
        if ($count >= $need_count) {
            $user->star = $star + 1;
            $user->save();
        }
    }

    protected function saveCommissionAwait($user_id)
    {
        $account = UserAccount::findOne(['user_id' => $user_id]);

        $account->balance += $account->await_balance;
        $account->await_balance = 0;
        $account->save();

        // 佣金状态
        $commissions = UserCommission::find()
            ->where(['user_id' => $user_id])
            ->andWhere(['status' => 0])
            ->all();
        foreach ($commissions as $c) {
            $c->status = 1;
            $c->save();
        }
    }
}